#!/usr/bin/env python3

from urllib import request,parse
from bs4 import BeautifulSoup
import time
import re
from pyquery import PyQuery
savePath = $0
bookName = $1

def search_book(bookname):
    url = 'http://www.biquge5200.com/modules/article/search.php?searchkey=' + parse.quote(bookname)
    response = request.urlopen(url)
    content = response.read().decode('gbk')
    soup = BeautifulSoup(content,'html.parser')
    menu = []
    key = 0
    for row in soup.find('table').find_all('tr'):
        td1 = row.select('td:nth-of-type(1)')
        td3 = row.select('td:nth-of-type(3)')
        if(td1 and td3):
            name    = td1[0].find('a').string
            href    = td1[0].find('a').get('href')
            author  = td3[0].string
            menu.append({'name':name,'href':href})
            print(str(key) + ' Novel：' + name + ' >> author：' + author)
            key += 1
    if(menu):
        select_key = -1
        while(select_key >= key or select_key < 0):
            select_key = int(input('Novel number：'))
        return menu[int(select_key)]
    return []

def get_novel_menu(url):
    response = request.urlopen(url)
    content = response.read().decode('gbk')
    soup = BeautifulSoup(content, 'html.parser')
    list = []
    for dd in soup.find('div',id="list").find('dt').find_next('dt').find_all_next('dd'):
        title = dd.find('a').string
        href = dd.find('a').get('href')
        list.append({'title':title,'href':href})
    return list
def get_response(url, times = 0):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
        }
        response = request.urlopen(request.Request(url,headers=headers))

        return response
    except:
        times += 1
        print("请求超时，重新请求中。。。", times , " -- ", url)
        sleep(1)
        return get_response(url, time)

def get_novel_content(title,url):
    doc = PyQuery(url)
    content = doc('#content')
    text = doc('#content').html().replace('<br/>', "\r\n\r\n").replace('<.*?>', '')
    return '\r\n' + title + "\r\n" + text

info = []

while(info == []):
    savePath = input('Save Path: ')
    bookName = input('novel name： ')
    info = search_book(bookName)
menu_lists = get_novel_menu(info['href'])

if(menu_lists == []):
    print('Sorry!I can\'t find this novel list')
    exit(0)
else:
    f = open(savePath, 'w')
    f.write('')
    f.close()

for list in menu_lists:
    title = list['title']
    newTitle = re.sub('^(\d+)', "第\\1章", title)
    print('Downloading： ' + newTitle + list['href'])
    content = get_novel_content(newTitle,list['href'])
    f = open(savePath, 'a')
    f.write(content)
    f.close()
    time.sleep(0.5)